<?php

namespace app\api\controller;

use addons\qcloudsms\Qcloudsms;
use app\admin\model\BankCard;
use app\admin\model\ClassAddress;
use app\admin\model\Club;
use app\admin\model\ClubDtl;
use app\admin\model\Notice;
use app\admin\model\TeacherClass;
use app\admin\model\TeacherClassDtl;
use app\admin\model\TeacherCourseDisplay;
use app\admin\model\Withdraw;
use app\common\controller\Api;
use app\common\model\MoneyLog;

/**
 * 钱包接口
 */
class WalletApi extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    //收支明细
    public function money_list(){
        $params = $this->request->param();
        if(!isset($params['page'])||!$params['page']){
            $params['page'] = 1;
        }
        if(!isset($params['page_size'])||!$params['page_size']){
            $params['page_size'] = 10;
        }

        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $query = MoneyLog::where(['user_id'=>$user['id']])->order('id','desc')->paginate($params['page_size']);

        $list = $query->getCollection();
        $arr = [];
        foreach ($list as $k=>$v){
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['memo'] = $v['memo'];
            $arr[$k]['money'] = $v['money'];
            $arr[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $count = $query->total();
        $data['money'] = number_format($user['money'],2);

        $bank = BankCard::where(['user_id'=>$user['id']])->find();
        $data['bank_name'] = isset($bank['bank_name']) ? $bank['bank_name'] : '';
        $data['card_number'] = isset($bank['card_number']) ? $bank['card_number'] : '';
        $data['name'] = isset($bank['name']) ? $bank['name'] : '';

        $data['list'] = $arr;
        $data['count'] = $count;

        $this->success('余额变动列表',$data);
    }

    //修改银行卡信息
    public function bank_card_up(){
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }
        $params = $this->request->param();
        $update = [];
        if(isset($params['bank_name'])&&$params['bank_name']){
            $update['bank_name'] = $params['bank_name'];
        }
        if(isset($params['card_number'])&&$params['card_number']){
            $update['card_number'] = $params['card_number'];
        }
        if(isset($params['name'])&&$params['name']){
            $update['name'] = $params['name'];
        }
        if(count($update)){
            $find = BankCard::where(['user_id'=>$user['id']])->find();
            $update['uptime'] = date('Y-m-d H:i:s');
            if($find){
                BankCard::where(['user_id'=>$user['id']])->update($update);
            }else{
                $update['user_id'] = $user['id'];
                BankCard::where(['user_id'=>$user['id']])->insert($update);
            }
            $this->success('修改完成',$params);
        }else{
            $this->error('无变动');
        }
    }

    //提现申请
    public function withdraw(){
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $bank = BankCard::where(['user_id'=>$user['id']])->find();
        if($user['money']<=0){
            $this->error('金额大于0才可以提现');
        }
        $add = [];
        $add['user_id'] = $user['id'];//用户id
        $add['money'] = $user['money'];//提现金额
        $add['card_number'] = $bank['card_number'];//银行卡号
        $add['bank_name'] = $bank['bank_name'];//银行名称
        $add['name'] = $bank['name'];//姓名
        $add['addtime'] = date('Y-m-d H:i:s');//申请时间
        $add['status'] = 1;//状态 1申请中 2通过 3驳回
        Withdraw::insert($add);
        //扣钱
        \app\admin\model\User::where(['id'=>$user['id']])->update(['money'=>0]);
        //记录
        $money_log = [];
        $money_log['user_id'] = $user['id'];//会员ID
        $money_log['money'] = 0-$user['money'];//变更余额
        $money_log['before'] = $user['money'];//变更前余额
        $money_log['after'] = 0;//变更后余额
        $money_log['memo'] = '提现金额';//备注
        $money_log['createtime'] = time();//创建时间
        $money_log['order_id'] = 0;//订单id
        MoneyLog::insert($money_log);
        //todo::这里应该发通知，但是目前不知道通知谁

//        $mobile = ['18279539321'];
//        foreach ($mobile as $v){
//            $fail_reason = '';
//            //腾讯云
//            $params = [
//                'template_id' => 2445619,
//                'mobile' => $v,
//                'code'=>[]
//            ];
//            $Qcloudsms = new Qcloudsms();
//            $res = $Qcloudsms->smsSend($params);
//            if (!$res) {
//                $fail_reason = $Qcloudsms->getError();
//            }
//        }

        $this->success('申请中','申请提现'.$user['money']);
    }

}
